package com.shortchainsystem.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

public class TransationObject {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    /**
     *  Json序列化
     */
    public static <T> String serialize(T object) {
        try {
            objectMapper.registerModule(new JavaTimeModule());
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Serialization error: ", e);
        }
    }

    /**
     *  Json反序列化
     */
    public static <T> T deserialize(String json, Class<T> clazz) {
        try {
            objectMapper.registerModule(new JavaTimeModule());
            return objectMapper.readValue(json, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Deserialization error: ", e);
        }
    }

}
